<?php

namespace app\model\user;

use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

class UserShareInviteLog extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 用户分享邀请记录添加
     * @param $param
     * @return int|string
     */
    public static function addUserShareInvateLog(int $share_pid, $type=1, int $integral, int $new_uid)
    {
        $add = [
            "uid"           => $share_pid ?? 0,
            "type"          => $type,
            "integral"      => $integral ?? 0,
            "new_uid"       => $new_uid ?? 0,
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     * 用户邀请记录列表
     * @param $params
     * @return array
     */
    public static function getUserShareInviteList($params = [])
    {
        $data["data"] = self::alias("a")
            ->field("a.*,FROM_UNIXTIME(a.create_time) as create_time,c.nickname,c.avatar,c.phone,c.real_name,
                case when a.type=1 then '邀新注册'
                end as type_text
            ")
            ->join('user c', 'a.new_uid=c.uid','left')
            ->where(function ($query) use ($params){
                $query->whereNull("a.delete_time");
                $query->where("a.uid", $params['user_id']);
            })
            ->order(['a.id' => 'desc'])
            ->page($params["page"], $params['limit'])
            ->select()->toArray();

        $data["sum_integral"] = self::where("uid", $params['user_id'])->whereNull("delete_time")->sum("integral")  ?? 0;

        $data["count"] = self::alias("a")
            ->field("a.*")
            ->join('user c', 'a.uid=c.uid','left')
            ->where(function ($query) use ($params){
                $query->whereNull("a.delete_time");
                $query->where("a.uid", $params['user_id']);
            })
            ->count();

        return $data;
    }



}